package com.ph.irp.base.salesprice.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.ph.irp.base.salesprice.dao.PBaseSalespriceDao;
import com.ph.irp.base.salesprice.service.PBaseSalespriceService;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectCustomerDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectMaterialDao;
import com.ph.irp.dto.SalespriceDTO;
import com.ph.irp.model.PBaseSalesprice;
import com.ph.irp.model.PBaseVerifyprojectCustomer;
import com.ph.irp.model.PBaseVerifyprojectMaterial;

public class PBaseSalespriceServiceImpl implements PBaseSalespriceService {

	int iRet = 0;
	private PBaseSalespriceDao salespriceDaoProxy;
	private PBaseVerifyprojectMaterialDao materialDaoProxy;
	private PBaseVerifyprojectCustomerDao customerDaoProxy;

	private List<SalespriceDTO> salespriceDTOList;

	@Override
	public int addSalesprice(PBaseSalesprice salespriceModel) throws Throwable {
		if (salespriceModel != null) {
			salespriceDaoProxy.addSalesprice(salespriceModel);
			iRet = 1;
		}
		return iRet;
	}

	@Override
	public int delectSalesprice(Integer salespriceId) throws Throwable {
		if (salespriceId != null) {
			salespriceDaoProxy.delectSalesprice(salespriceId);
			iRet = 1;
		}
		return iRet;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SalespriceDTO> getAllSalespriceList() throws Throwable {
		salespriceDTOList = new ArrayList<SalespriceDTO>();
		List<PBaseSalesprice> salespriceList;
		salespriceList = salespriceDaoProxy.getAllSalespriceList();
		Iterator it = salespriceList.iterator();
		while (it.hasNext()) {
			PBaseSalesprice salesprice = (PBaseSalesprice) it.next();
			SalespriceDTO salespriceDTO = new SalespriceDTO();
			salespriceDTOList = new ArrayList<SalespriceDTO>();
			salespriceDTO.setSalesprice(salesprice);
			int materialId = salesprice.getMaterialId();
			int customerId = salesprice.getCustomerId();
			PBaseVerifyprojectMaterial material = materialDaoProxy
					.getMaterialById(materialId);
			salespriceDTO.setMaterial(material);
			PBaseVerifyprojectCustomer customer = customerDaoProxy
					.getCustomerById(customerId);
			salespriceDTO.setCustomer(customer);
			salespriceDTOList.add(salespriceDTO);
		}
		return salespriceDTOList;
	}

	@Override
	public PBaseSalesprice getSalespriceByID(Integer salespriceId)
			throws Throwable {
		return salespriceDaoProxy.getSalespriceByID(salespriceId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SalespriceDTO> getSalespriceByModel(
			PBaseSalesprice salespriceModel) throws Throwable {
		salespriceDTOList = new ArrayList<SalespriceDTO>();
		List<PBaseSalesprice> salespriceList;
		salespriceList = salespriceDaoProxy
				.getSalespriceByModel(salespriceModel);
		Iterator it = salespriceList.iterator();
		while (it.hasNext()) {
			PBaseSalesprice salesprice = (PBaseSalesprice) it.next();
			SalespriceDTO salespriceDTO = new SalespriceDTO();
			salespriceDTO.setSalesprice(salesprice);
			int materialId = salesprice.getMaterialId();
			int customerId = salesprice.getCustomerId();
			PBaseVerifyprojectMaterial material = materialDaoProxy
					.getMaterialById(materialId);
			salespriceDTO.setMaterial(material);
			PBaseVerifyprojectCustomer customer = customerDaoProxy
					.getCustomerById(customerId);
			salespriceDTO.setCustomer(customer);
			salespriceDTOList.add(salespriceDTO);
		}
		return salespriceDTOList;
	}

	@Override
	public int updateSalesprice(PBaseSalesprice salespriceModel)
			throws Throwable {
		if (salespriceModel != null) {
			salespriceDaoProxy.updateSalesprice(salespriceModel);
			iRet = 1;
		}
		return iRet;
	}

	public List<SalespriceDTO> getSalespriceDTOList() {
		return salespriceDTOList;
	}

	public void setSalespriceDTOList(List<SalespriceDTO> salespriceDTOList) {
		this.salespriceDTOList = salespriceDTOList;
	}

	public PBaseSalespriceDao getSalespriceDaoProxy() {
		return salespriceDaoProxy;
	}

	public void setSalespriceDaoProxy(PBaseSalespriceDao salespriceDaoProxy) {
		this.salespriceDaoProxy = salespriceDaoProxy;
	}

	public PBaseVerifyprojectMaterialDao getMaterialDaoProxy() {
		return materialDaoProxy;
	}

	public void setMaterialDaoProxy(
			PBaseVerifyprojectMaterialDao materialDaoProxy) {
		this.materialDaoProxy = materialDaoProxy;
	}

	public PBaseVerifyprojectCustomerDao getCustomerDaoProxy() {
		return customerDaoProxy;
	}

	public void setCustomerDaoProxy(
			PBaseVerifyprojectCustomerDao customerDaoProxy) {
		this.customerDaoProxy = customerDaoProxy;
	}

}
